﻿using System;
using System.Linq;
using System.Management;
using ClusterAware.Properties;
using ClusterAwareProperties;
using ClusterAwareWMI;

namespace ClusterAware
{
    /// <summary>
    /// Wrapper property to Resource Class actions
    /// </summary>
    public class ResourceMethods
    {

        #region private properties

        private readonly ClusterManager _managerclass;

        #endregion

        #region Ctor

        /// <summary>
        /// private Constructor
        /// </summary>
        private ResourceMethods()
        {
        }

        /// <summary>
        /// Create Class ResourceMethods
        /// </summary>
        /// <param name="managerclass">Instance of ClusterManager class</param>
        public ResourceMethods(ClusterManager managerclass)
            : this()
        {
            _managerclass = managerclass;
        }

        #endregion

        #region public Methods

        /// <summary>
        /// CusterManager Instance
        /// </summary>
        public ClusterManager ManagerInstance
        {
            get { return _managerclass; }
        }

        /// <summary>
        /// if Exists Resource
        /// </summary>
        /// <param name="resourceName">Resource name</param>
        /// <returns>True if exists</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter resource name is empty
        /// </exception>
        public bool ExistResource(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (_managerclass.CacheInstances)
            {
                return _managerclass.ResourceInstances.ContainsKey(resourceName);
            }
            try
            {
                var res = new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
                return (res != null);
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// get list of resource 
        /// </summary>
        /// <returns>Array of class resource</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected
        /// </exception>
        public ResourceProperty[] GetResources()
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            try
            {
                return _managerclass.CacheInstances ? _managerclass.ResourceInstances.Values.ToArray() : new ClusterWmiHelpper().GetResources(_managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetResources", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetResources", ex.Message), ex);
            }
        }

        /// <summary>
        /// Get a resource
        /// </summary>
        /// <param name="resourceName">name resource</param>
        /// <returns>Resource class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter resource name is empty, 
        /// Resource not exists, 
        /// </exception>
        public ResourceProperty GetResource(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (_managerclass.CacheInstances)
            {
                if (!_managerclass.ResourceInstances.ContainsKey(resourceName))
                {
                    throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resourceName));
                }
                return _managerclass.ResourceInstances[resourceName];
            }
            try
            {
                return new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetResource", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetResource", ex.Message), ex);
            }
        }



        /// <summary>
        /// Take a Resource offline.
        /// </summary>
        /// <param name="resourceName">Resource name</param>
        /// <param name="segtimeout">The length of time (in seconds) that the method should wait for the resource to go offline.</param>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter resource name is empty, 
        /// Resource not exists, 
        /// Failed WMI method execution
        /// </exception>
        public void TakeOffline(string resourceName, UInt32 segtimeout)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (!ExistResource(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resourceName));
            }
            try
            {
                var res = _managerclass.CacheInstances ? _managerclass.ResourceInstances[resourceName] : new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
                using (var inParams = res.Instance.GetMethodParameters("TakeOffline"))
                {
                    inParams["TimeOut"] = segtimeout;
                    res.Instance.InvokeMethod("TakeOffline", inParams, null);
                    if (_managerclass.CacheInstances)
                    {
                        _managerclass.Refresh(RefreshOption.Resources, resourceName);
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.TakeOffline", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.TakeOffline", ex.Message), ex);
            }
        }

        /// <summary>
        /// Brings a resource online.
        /// </summary>
        /// <param name="resourceName">Resource name</param>
        /// <param name="segtimeout">The length of time (in seconds) that the method should wait for the resource to come online.</param>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter resource name is empty, 
        /// Resource not exists, 
        /// Failed WMI method execution
        /// </exception>
        public void BringOnline(string resourceName, UInt32 segtimeout)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (!ExistResource(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resourceName));
            }
            try
            {
                var res = _managerclass.CacheInstances ? _managerclass.ResourceInstances[resourceName] : new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
                using (var inParams = res.Instance.GetMethodParameters("BringOnline"))
                {
                    inParams["TimeOut"] = segtimeout;
                    res.Instance.InvokeMethod("BringOnline", inParams, null);
                    if (_managerclass.CacheInstances)
                    {
                        _managerclass.Refresh(RefreshOption.Resources, resourceName);
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.BringOnline", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.BringOnline", ex.Message), ex);
            }
        }

        ///<summary>
        /// Save a Resource in Cluster
        ///</summary>
        ///<param name="resClass">Resource Class to Save</param>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Resource not exists, 
        /// Failed WMI method execution
        /// </exception>
        /// <returns>
        /// ResourceProperty Class
        /// </returns>
        public ResourceProperty Save(ResourceProperty resClass)
        {
            return Save(resClass, false, 0);
        }

        ///<summary>
        /// Save a Resource in Cluster
        ///</summary>
        ///<param name="resClass">Resource Class to Save</param>
        ///<param name="forceRestart">if resource online execute Takeoffline before save and Bringonline after save</param>
        ///<param name="timeoutRestart">The length of time (in seconds) that the method should wait for the resource to execute restart.</param>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Resource not exists, 
        /// Failed WMI method execution
        /// </exception>
        /// <returns>
        /// ResourceProperty Class
        /// </returns>
        public ResourceProperty Save(ResourceProperty resClass, bool forceRestart, UInt32 timeoutRestart)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (resClass == null)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "Resclass"));
            }
            if (!ExistResource(resClass.Name))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resClass.Name));
            }
            try
            {
                var isonline = false;
                if (forceRestart)
                {
                    if (resClass.State == StateResources.Online)
                    {
                        isonline = true;
                        TakeOffline(resClass.Name, timeoutRestart);
                    }
                }
                resClass.SaveManagementObject(_managerclass.OperationSystem);
                if (isonline)
                {
                    BringOnline(resClass.Name, timeoutRestart);
                }
                var ret = new ClusterWmiHelpper().GetResource(resClass.Name, _managerclass.ScopeManager);
                if (_managerclass.CacheInstances)
                {
                    _managerclass.ResourceInstances[resClass.Name] = ret;
                }
                return ret;
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.Save", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.Save", ex.Message), ex);
            }
        }


        ///<summary>
        /// Moves the resource to a different group.
        ///</summary>
        ///<param name="resourceName">The resource name</param>
        ///<param name="newGroup">The name of the group to receive the resource.</param>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameters empty
        /// Group not exist
        /// Resource not exist
        /// Failed WMI method execution
        /// </exception>
        public void MoveToNewGroup(string resourceName, string newGroup)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (string.IsNullOrEmpty(newGroup))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "newgroup"));
            }
            if (!ExistResource(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resourceName));
            }
            if (!_managerclass.Groups.ExistGroup(newGroup))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Group", newGroup));
            }
            try
            {
                var res = _managerclass.CacheInstances ? _managerclass.ResourceInstances[resourceName] : new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
                using (var inParams = res.Instance.GetMethodParameters("MoveToNewGroup"))
                {
                    inParams["Group"] = newGroup;
                    res.Instance.InvokeMethod("MoveToNewGroup", inParams, null);
                    if (_managerclass.CacheInstances)
                    {
                        _managerclass.Refresh(RefreshOption.Resources, resourceName);
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.MoveToNewGroup", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.MoveToNewGroup", ex.Message), ex);
            }
        }


        ///<summary>
        /// Gets the resource dependency expression.
        ///</summary>
        ///<param name="resourceName">The resource name</param>
        /// <remarks>
        /// This method is not supported before Windows Server 2008.
        /// </remarks>
        ///<returns>The expression that represents the resource dependencies.</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameters empty
        /// Resource not exist
        /// Operarion System before Windows Server 2008
        /// Failed WMI method execution
        /// </exception>
        public string GetDependencieseExpression(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (_managerclass.OperationSystem == WindowsOperationSystem.Windows2003 || _managerclass.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_SystemNotSuportMethod, _managerclass.OperationSystem, "GetDependencies"));
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (!ExistResource(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resourceName));
            }
            string ret;
            try
            {
                var res = _managerclass.CacheInstances ? _managerclass.ResourceInstances[resourceName] : new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
                // ReSharper disable AssignNullToNotNullAttribute
                ManagementBaseObject outParams = res.Instance.InvokeMethod("GetDependencies", null, null);
                // ReSharper restore AssignNullToNotNullAttribute
                // ReSharper disable PossibleNullReferenceException
                ret = (string)outParams["Expression"];
                // ReSharper restore PossibleNullReferenceException
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetDependencies", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetDependencies", ex.Message), ex);
            }
            return ret;
        }

        /// <summary>
        /// Get list Dependencies Resource of a Resource
        /// </summary>
        /// <param name="resourceName">name resource</param>
        /// <returns>Array group class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter node name is empty, 
        /// Resource not exists
        /// </exception>
        /// <remarks>
        /// Not Cachebled result
        /// </remarks>
        public ResourceProperty[] GetDependenciesResource(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            try
            {
                return new ClusterWmiHelpper().FindDependenciesResourceByResource(resourceName, _managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetDependenciesResource", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetDependenciesResource", ex.Message), ex);
            }
        }


        /// <summary>
        /// Get list Disk of a Resource
        /// </summary>
        /// <param name="resourceName">name resource</param>
        /// <returns>Array Disk class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter node name is empty, 
        /// Operarion System before Windows 2008,
        /// Resource not exists
        /// </exception>
        /// <remarks>
        /// Not Cachebled result
        /// </remarks>
        public DiskProperty[] GetDisks(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (_managerclass.OperationSystem == WindowsOperationSystem.Windows2003 || _managerclass.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_SystemNotSuportMethod, _managerclass.OperationSystem, "GetDisks"));
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            try
            {
                return new ClusterWmiHelpper().FindDisksByResource(resourceName, _managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetDisks", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetDisks", ex.Message), ex);
            }
        }

        /// <summary>
        /// Get list possible owner Node of a Resource
        /// </summary>
        /// <param name="resourceName">name resource</param>
        /// <returns>Array Node class</returns>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameter node name is empty, 
        /// Resource not exists
        /// </exception>
        /// <remarks>
        /// Not Cachebled result
        /// </remarks>
        public NodeProperty[] GetPossibleOwnerNode(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            try
            {
                return new ClusterWmiHelpper().FindPossibleOwnerNodesByResource(resourceName, _managerclass.ScopeManager);
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetPossibleOwnerNode", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.GetPossibleOwnerNode", ex.Message), ex);
            }
        }

        ///<summary>
        /// Forces this resource to become unavailable to simulate failure. Applications use this method to test their failover configurations.
        ///</summary>
        ///<param name="resourceName">The resource name</param>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameters empty
        /// Resource not exist
        /// Failed WMI method execution
        /// </exception>
        public void FailResource(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (!ExistResource(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resourceName));
            }
            try
            {
                var res = _managerclass.CacheInstances ? _managerclass.ResourceInstances[resourceName] : new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
                using (var inParams = res.Instance.GetMethodParameters("FailResource"))
                {
                    try
                    {
                        res.Instance.InvokeMethod("FailResource", inParams, null);
                    }
                    catch (ManagementException ex)
                    {
                        if (ex.ErrorCode != ManagementStatus.Failed)
                        {
                            throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.FailResource", ex.Message), ex);
                        }
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.FailResource", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.FailResource", ex.Message), ex);
            }
        }

        ///<summary>
        /// Renews the IPv4 address DHCP lease. Valid only for IP address resources.
        ///</summary>
        ///<param name="resourceName">The resource name</param>
        /// <remarks>
        /// This method is not supported before Windows Server 2008.
        /// </remarks>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameters empty
        /// Resource not exist
        /// Operarion System before Windows Server 2008
        /// Failed WMI method execution
        /// </exception>
        public void RenewAddress(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (_managerclass.OperationSystem == WindowsOperationSystem.Windows2003 || _managerclass.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_SystemNotSuportMethod, _managerclass.OperationSystem, "RenewAddress"));
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (!ExistResource(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resourceName));
            }
            try
            {
                var res = _managerclass.CacheInstances ? _managerclass.ResourceInstances[resourceName] : new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
                using (var inParams = res.Instance.GetMethodParameters("RenewAddress"))
                {
                    res.Instance.InvokeMethod("RenewAddress", inParams, null);
                    if (_managerclass.CacheInstances)
                    {
                        _managerclass.Refresh(RefreshOption.Resources, resourceName);
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.RenewAddress", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.RenewAddress", ex.Message), ex);
            }
        }

        ///<summary>
        /// Releases the IPv4 address DHCP lease. Valid only for IP address resources.
        ///</summary>
        ///<param name="resourceName">The resource name</param>
        /// <remarks>
        /// This method is not supported before Windows Server 2008.
        /// </remarks>
        ///<exception cref="ClusterManagerException">
        /// this method throws exception for the following conditions:
        /// Not Connected, 
        /// Parameters empty
        /// Resource not exist
        /// Operarion System before Windows Server 2008
        /// Failed WMI method execution
        /// </exception>
        public void ReleaseAddress(string resourceName)
        {
            if (!_managerclass.IsConnected)
            {
                throw new ClusterManagerException(Resources.Error_NotConnected);
            }
            if (_managerclass.OperationSystem == WindowsOperationSystem.Windows2003 || _managerclass.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_SystemNotSuportMethod, _managerclass.OperationSystem, "ReleaseAddress"));
            }
            if (string.IsNullOrEmpty(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamEmpty, "resourceName"));
            }
            if (!ExistResource(resourceName))
            {
                throw new ClusterManagerException(string.Format(Resources.Error_ParamNotExist, "Resource", resourceName));
            }
            try
            {
                var res = _managerclass.CacheInstances ? _managerclass.ResourceInstances[resourceName] : new ClusterWmiHelpper().GetResource(resourceName, _managerclass.ScopeManager);
                using (var inParams = res.Instance.GetMethodParameters("ReleaseAddress"))
                {
                    res.Instance.InvokeMethod("ReleaseAddress", inParams, null);
                    if (_managerclass.CacheInstances)
                    {
                        _managerclass.Refresh(RefreshOption.Resources, resourceName);
                    }
                }
            }
            catch (ManagementException ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.ReleaseAddress", ex.Message), ex);
            }
            catch (Exception ex)
            {
                throw new ClusterManagerException(string.Format(Resources.Error_MethodExecute, "Resources.ReleaseAddress", ex.Message), ex);
            }
        }

        #endregion
    }
}
